'use strict';
angular
    .module('myApp.vmoiveList', [
        'ngRoute',
        'moviecat.services.http',
    ])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/:type/:page', {
            templateUrl: 'view/movieList/view.html',
            controller: 'moiveListCtrl'
        });
    }])
    .controller('moiveListCtrl', ['$scope',
        '$interval',
        'HttpService',
        '$route',
        '$routeParams',
        'moiveConfig',
        function($scope, $interval, HttpService, $route, $routeParams, moiveConfig) {
            $scope.title = 'Loading...';
            $scope.films = [];
            $scope.totalCount = 0;
            $scope.progress = 0;
            $scope.complete = true;
            $scope.CurrentPage = 0;
            // 进度条
            $interval(() => {
                $scope.progress += Math.round(20 + Math.random() * 10);
            }, 1000);
            // 分页
            let PageSize = parseInt(moiveConfig.pageSize);
            let CurrentPage = parseInt($routeParams.page);
            $scope.totalPage = 0;

            HttpService.jsonp(`${moiveConfig.moiveUrl}${$routeParams.type}`, {
                    start: (CurrentPage - 1) * PageSize,
                    count: PageSize,
                    q: $routeParams.q
                },
                function(res) {
                    $scope.title = res.title;
                    $scope.films = res.subjects;
                    $scope.totalCount = res.total;
                    $scope.CurrentPage = CurrentPage;
                    $scope.totalPage = Math.ceil(parseInt(res.total) / parseInt(PageSize));
                    $scope.progress = 100;
                    $scope.complete = false;
                    $scope.$apply();
                });
            $scope.go = function(page) {
                if (page >= 1 && page <= $scope.totalPage)
                    $route.updateParams({
                        page: page
                    })
            }
        }
    ]);